<?php
namespace app\material\controller\sys_admin;
use app\AdminController;

use app\material\model\MaterialCategoryModel;
use app\material\model\MaterialListModel;
use app\shop\model\GoodsModel;
use think\Db;

//use function app\school\controller\sys_admin\makeDir;

/**
 * 高清大图
 */
class Audit extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new MaterialListModel();
    }
	/*------------------------------------------------------ */
	//-- 主页
	/*------------------------------------------------------ */
    public function index(){

        $this->getList(true);
		return $this->fetch();
	}
    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false)
    {
        $where = [];
        $where[] = ['is_audit', 'in', [0,2]];
        $where[] = ['is_del','=',0];
        $search['keyword'] = input('keyword', '', 'trim');
        $search['type'] = input('type', '', 'trim');
        if (empty($search['keyword']) == false) $where[] = ['name', 'like', '%' . $search['keyword'] . '%'];
        if (empty($search['type']) == false) $where[] = ['type', '=', $search['type']];
        $data = $this->getPageList($this->Model, $where);
        $cateModel = new MaterialCategoryModel();
        foreach ($data['list'] as &$value){
            $value['add_time'] = date('Y-m-d H:i',$value['add_time']);
            $value['cate_name'] = $cateModel->where(['id' => $value['cid']])->value('name');
            if($value['images']){
                $value['images'] = explode(',',$value['images']);
            }
            if($value['video'] && !$value['goods_id']){
                $value['type_text'] = '视频';
            }elseif($value['content'] && $value['images']){
                $value['type_text'] = '图文';
            }elseif($value['goods_id'] && $value['video']){
                $value['type_text'] = '带货视频';
            }else{
                $value['type_text'] = '图片';
            }

        }
        $this->assign("data", $data);
        $this->assign("search", $search);
        if ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }

    public function asInfo($data)
    {
        $cateModel = new MaterialCategoryModel();
        $goodsModel = new GoodsModel();
        $cate_list = $cateModel->where(['is_show' => 1])->select();
        if($data['images']){
            $image_list = explode(',',$data['images']);
        }else{
            $image_list = [];
        }
        $data['goods_img'] = $goodsModel->where(['goods_id' => $data['goods_id']])->value('goods_img');
        $this->assign('cate_list', $cate_list);
        $this->assign('image_list', $image_list);
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 验证数据
    /*------------------------------------------------------ */
    private function checkData($data) {
//        if (empty($data['name'])){
//            $this->error('请填写分类名称');
//        }
//        if (empty($data['image'])){
//            $this->error('请上传图片.');
//        }
//        $image = $data['image'];
//        if (strstr($image,'snap_file')){
//            $file_path = config('config._upload_').'school/hdimg/';
//            $data['image'] = copyFile($image,$file_path);
//            @unlink('.'.$image);
//            if ($data['id'] > 0){
//                $oldimg = $this->Model->where('id',$data['id'])->value('image');
//                @unlink('.'.$oldimg);
//            }
//        }
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 添加前调用
    /*------------------------------------------------------ */
    public function beforeAdd($data)
    {
        $data['add_time'] = time();
        $data['is_audit'] = 1;
        $img_list = [];
        if(isset($data['images']['path']) && $data['images']['path']){
            foreach ($data['images']['path'] as $value){
                $img_list[] = $value;
            }
        }
        $data['images'] = $img_list ? implode(',',$img_list) : '';
        return $this->checkData($data);
    }
    //-- 添加后调用
    /*------------------------------------------------------ */
    public function afterAdd($data)
    {
        return $this->success('添加成功.', url('index'));
    }
    /*------------------------------------------------------ */
    //-- 修改前调用
    /*------------------------------------------------------ */
    public function beforeEdit($data)
    {
        $img_list = [];
        if(isset($data['images']['path']) && $data['images']['path']){
            foreach ($data['images']['path'] as $value){
                $img_list[] = $value;
            }
        }
        $data['images'] = $img_list ? implode(',',$img_list) : '';
        return $this->checkData($data);
    }
    /*------------------------------------------------------ */
    //-- 修改后调用
    /*------------------------------------------------------ */
    public function afterEdit($data)
    {
        return $this->success('修改成功.');
    }
    /*------------------------------------------------------ */
    //-- 删除
    /*------------------------------------------------------ */
    public function del()
    {
        $map['id'] = input('id', 0, 'intval');
        if ($map['id'] < 1) return $this->error('传递参数失败！');
        $row = $this->Model->where($map)->find();
        $res = $this->Model->where($map)->delete();
        if ($res < 1) return $this->error();
        @unlink('.'.$row['image']);
        return $this->success('删除成功.');
    }

    public function audit()
    {
        $id = input('id', 0, 'intval');
        $row = $this->Model->where('id',$id)->find();
        if ($this->request->isPost()) {
            $is_audit = input('is_audit', '');
            $audit_reason = input('audit_reason', '');
            $price = input('price', 0);
            $res = $this->Model->where('id',$id)->update([
                'is_audit' => $is_audit,
                'price' => $price,
                'status' => $is_audit == 1 ? 1 : 0,
                'audit_reason' => $audit_reason
            ]);
            if ($res < 1) return $this->error('操作失败,请重试.');
            return $this->success('审核成功！', 'reload');
        }
        $this->assign("row", $row);

        return $this->fetch('sys_admin/audit/audit');

    }
}
